package org.dreamwork.sso.saml2.protocol;

import org.opensaml.saml2.core.*;
import org.opensaml.saml2.core.impl.NameIDBuilder;
import org.opensaml.saml2.core.impl.SubjectConfirmationBuilder;
import org.opensaml.xml.XMLObjectBuilderFactory;
import org.w3c.dom.Element;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: seth.yang
 * Date: 13-8-19
 * Time: 下午1:19
 */
public class SamlSubject extends SamlObject<Subject> {
    protected Subject subject;

    public SamlSubject (XMLObjectBuilderFactory factory) {
        super (factory);

        subject = create (Subject.class);
    }

    @Override
    public Element getDom () {
        return subject.getDOM ();
    }

    protected Subject getSubject () {
        return subject;
    }

    public String getNameId () {
        NameID id = subject.getNameID ();
        return id == null ? null : id.getValue ();
    }

    public void setNameId (String nameId) {
        NameID id = subject.getNameID ();
        if (id == null) {
            id = new NameIDBuilder ().buildObject ();
            subject.setNameID (id);
        }
        id.setValue (nameId);
    }

    public void addSubjectConfirmation (String method) {
        List<SubjectConfirmation> list = subject.getSubjectConfirmations ();
        if (list != null) {
            SubjectConfirmation sc = new SubjectConfirmationBuilder ().buildObject ();
            sc.setMethod (method);
            list.add (sc);
        }
    }
}